/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import util.HibernateUtil;
import java.util.List;

import pojo.Chitietdonhang;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author Jackhit
 */
public class ChiTietDonHangDAO {
    public static boolean themChiTietDonHang(Chitietdonhang chiTietDonHang) {
       Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.save(chiTietDonHang);
            session.getTransaction().commit();
            return true;
        }catch(Exception e) {
            session.getTransaction().rollback();
            return false;
        }
        finally {
            session.close();
        }
    }

    public static boolean capNhatChiTietDonHang(Chitietdonhang chiTietDonHang) {
       Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.update(chiTietDonHang);
            session.getTransaction().commit();
            return true;
        }catch(Exception e) {
            session.getTransaction().rollback();
            return false;
        }
        finally {
            session.close();
        }
    }

    public static List<Chitietdonhang> layDanhSachChiTietDonHangTheoDonHang(int maDonHang) {
        List<Chitietdonhang> dsCTDonHang = null;
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            String hql = "from Chitietdonhang ct where ct.donHang.MaDonHang = :maDonHang";
            Query query = session.createQuery(hql);
            query.setString("maDonHang", ""+maDonHang);
            dsCTDonHang = query.list();
         }catch(Exception ex) {
            System.out.println(ex.getMessage());
        }finally {
            session.close();
        }
        return dsCTDonHang;
    }
}
